import getUserTempId from '@/utils/userTempId'
import {requestLogin,requestGetUserInfo,requestLogout} from '@/api'

const state = {
    userTempId:getUserTempId(),
    token:localStorage.getItem('user_token') || '',
    nickName:''
}
const mutations = {
    SET_TOKEN_STATE(state,value){
        state.token = value
    },
    SET_NICKNAME_STATE(state,value){
        state.nickName = value
    },
    SET_LOGOUT_STATE(state){
        state.token = ''
        state.nickName = ''
        localStorage.removeItem('user_token')
    }
}
const actions = {
    async getLogin({commit},userInfo){
        const re = await requestLogin(userInfo)
        commit('SET_TOKEN_STATE',re.token)
        localStorage.setItem('user_token',re.token)
    },
    async getUserInfo({commit}){
        const re = await requestGetUserInfo()
        commit('SET_NICKNAME_STATE',re.nickName)
    },
    async getLogout({commit}){
        await requestLogout()
        commit('SET_LOGOUT_STATE')
    }
}
const getters = {}

export default {
    namespaced:true,
    state,
    mutations,
    actions,
    getters
}